package com.zr.ssm.pojo;

import org.apache.ibatis.jdbc.SQL;

public class ArticalSql {
        /**
         * 动态sql的 sql 语句的拼接
         * @param : 接口的输入参数
         * @return: 就一个 sql语句
         */
        public String getDynamicSql(final Artical artical) {
            // 匿名内部类
            SQL sql = new SQL() {
                // 类的定义
                //
                // 代码块
                {
                    // select ev_articles.id,ev_articles.title, ev_articles.pub_date,ev_articles.state,ev_article_cate.name as cate_name
                    // from ev_articles, ev_article_cate where ev_articles.cate_id = ev_article_cate.id
                    SELECT("ev_articles.id,ev_articles.title, ev_articles.pub_date,ev_articles.state,ev_article_cate.name as cate_name");
                    FROM("ev_articles, ev_article_cate");
                    WHERE("ev_articles.cate_id = ev_article_cate.id");
                    if (artical.getCate_id() != null) {
                        WHERE("ev_articles.cate_id = #{cate_id}");
                    }
                    if (artical.getState() != null && !(artical.getState().isEmpty())) {
                        WHERE("ev_articles.state = #{state}");
                    }
                }
            };
            System.out.println(sql.toString());
            // 将 sql 对象 变成 sql语句
            return sql.toString();
        }

}
